﻿Imports System.Data.SqlClient
Partial Class Reportes_GenealogiaGral
    Inherits System.Web.UI.Page
    Dim padre As Integer
    Dim madre As Integer
    Dim salto As Integer = 0
    Private mG1, mG2T_1, mG2V_2, mG3T_1, mG3V_2, mG3T_3, mG3V_4, mG4T_1, mG4V_2, mG4T_3, mG4V_4, mG4T_5, mG4V_6, mG4T_7, mG4V_8 As Integer
    Private mG5T_1, mG5V_2, mG5T_3, mG5V_4, mG5T_5, mG5V_6, mG5T_7, mG5V_8, mG5T_9, mG5V_10, mG5T_11, mG5V_12, mG5T_13, mG5V_14, mG5T_15, mG5V_16 As Integer

    Protected Sub BtnInicio_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnInicio.Click
        Dim num As Integer
        Dim salida As Integer
        Dim CampoReg As String
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select * from animal"
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()
            num = Lector.HasRows
            While salida = 0


                If Lector.Read() Then
                    CampoReg = Lector("AnimalId")
                    Label2.Text = CampoReg
                    otorgarValor(CampoReg) 'OK
                Else
                    salida = 1
                End If
                
            End While
        Catch ex As Exception

        End Try
    End Sub
    Public Sub otorgarValor(ByVal CampoReg As Integer)
        Dim iIAnimal As Integer = CampoReg


        mG1 = iIAnimal
        ' Generacion 2 padres
        ClavesDePadres(iIAnimal)
        mG2T_1 = padre
        mG2V_2 = madre

        ' Generacion 3 Abuelos
        iIAnimal = mG2T_1
        ClavesDePadres(iIAnimal)
        mG3T_1 = padre
        mG3V_2 = madre
        iIAnimal = mG2V_2
        ClavesDePadres(iIAnimal)
        mG3T_3 = padre
        mG3V_4 = madre
        ' Generacion 4 Bisaabuelos
        iIAnimal = mG3T_1
        ClavesDePadres(iIAnimal)
        mG4T_1 = padre
        mG4V_2 = madre
        iIAnimal = mG3V_2
        ClavesDePadres(iIAnimal)
        mG4T_3 = padre
        mG4V_4 = madre
        iIAnimal = mG3T_3
        ClavesDePadres(iIAnimal)
        mG4T_5 = padre
        mG4V_6 = madre
        iIAnimal = mG3V_4
        ClavesDePadres(iIAnimal)
        mG4T_7 = padre
        mG4V_8 = madre
        ' Generación 5 TataAbuelos
        iIAnimal = mG4T_1
        ClavesDePadres(iIAnimal)
        mG5T_1 = padre
        mG5V_2 = madre
        iIAnimal = mG4V_2
        ClavesDePadres(iIAnimal)
        mG5T_3 = padre
        mG5V_4 = madre
        iIAnimal = mG4T_3
        ClavesDePadres(iIAnimal)
        mG5T_5 = padre
        mG5V_6 = madre
        iIAnimal = mG4V_4
        ClavesDePadres(iIAnimal)
        mG5T_7 = padre
        mG5V_8 = madre
        iIAnimal = mG4T_5
        ClavesDePadres(iIAnimal)
        mG5T_9 = padre
        mG5V_10 = madre
        iIAnimal = mG4V_6
        ClavesDePadres(iIAnimal)
        mG5T_11 = padre
        mG5V_12 = madre
        iIAnimal = mG4T_7
        ClavesDePadres(iIAnimal)
        mG5T_13 = padre
        mG5V_14 = madre
        iIAnimal = mG4V_8
        ClavesDePadres(iIAnimal)
        mG5T_15 = padre
        mG5V_16 = madre


        Dim SConnStr As String = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim cnBKTest As New SqlConnection(SConnStr)
        Dim cmdTest As New SqlCommand("SP_GenealogiaAgrega", cnBKTest)
        cmdTest.CommandType = Data.CommandType.StoredProcedure


        Dim G1 As SqlParameter = cmdTest.Parameters.Add("@G1", Data.SqlDbType.Int)
        G1.Direction = Data.ParameterDirection.Input
        Dim G2T_1 As SqlParameter = cmdTest.Parameters.Add("@G2T_1", Data.SqlDbType.Int)
        G2T_1.Direction = Data.ParameterDirection.Input
        Dim G2V_2 As SqlParameter = cmdTest.Parameters.Add("@G2V_2", Data.SqlDbType.Int)
        G2V_2.Direction = Data.ParameterDirection.Input

        Dim G3T_1 As SqlParameter = cmdTest.Parameters.Add("@G3T_1", Data.SqlDbType.Int)
        G3T_1.Direction = Data.ParameterDirection.Input
        Dim G3V_2 As SqlParameter = cmdTest.Parameters.Add("@G3V_2", Data.SqlDbType.Int)
        G3V_2.Direction = Data.ParameterDirection.Input
        Dim G3T_3 As SqlParameter = cmdTest.Parameters.Add("@G3T_3", Data.SqlDbType.Int)
        G3T_3.Direction = Data.ParameterDirection.Input
        Dim G3V_4 As SqlParameter = cmdTest.Parameters.Add("@G3V_4", Data.SqlDbType.Int)
        G3V_4.Direction = Data.ParameterDirection.Input


        Dim G4T_1 As SqlParameter = cmdTest.Parameters.Add("@G4T_1", Data.SqlDbType.Int)
        G4T_1.Direction = Data.ParameterDirection.Input
        Dim G4V_2 As SqlParameter = cmdTest.Parameters.Add("@G4V_2", Data.SqlDbType.Int)
        G4V_2.Direction = Data.ParameterDirection.Input
        Dim G4T_3 As SqlParameter = cmdTest.Parameters.Add("@G4T_3", Data.SqlDbType.Int)
        G4T_3.Direction = Data.ParameterDirection.Input
        Dim G4V_4 As SqlParameter = cmdTest.Parameters.Add("@G4V_4", Data.SqlDbType.Int)
        G4V_4.Direction = Data.ParameterDirection.Input
        Dim G4T_5 As SqlParameter = cmdTest.Parameters.Add("@G4T_5", Data.SqlDbType.Int)
        G4T_5.Direction = Data.ParameterDirection.Input
        Dim G4V_6 As SqlParameter = cmdTest.Parameters.Add("@G4V_6", Data.SqlDbType.Int)
        G4V_6.Direction = Data.ParameterDirection.Input
        Dim G4T_7 As SqlParameter = cmdTest.Parameters.Add("@G4T_7", Data.SqlDbType.Int)
        G4T_7.Direction = Data.ParameterDirection.Input
        Dim G4V_8 As SqlParameter = cmdTest.Parameters.Add("@G4V_8", Data.SqlDbType.Int)
        G4V_8.Direction = Data.ParameterDirection.Input

        Dim G5T_1 As SqlParameter = cmdTest.Parameters.Add("@G5T_1", Data.SqlDbType.Int)
        G5T_1.Direction = Data.ParameterDirection.Input
        Dim G5V_2 As SqlParameter = cmdTest.Parameters.Add("@G5V_2", Data.SqlDbType.Int)
        G5V_2.Direction = Data.ParameterDirection.Input
        Dim G5T_3 As SqlParameter = cmdTest.Parameters.Add("@G5T_3", Data.SqlDbType.Int)
        G5T_3.Direction = Data.ParameterDirection.Input
        Dim G5V_4 As SqlParameter = cmdTest.Parameters.Add("@G5V_4", Data.SqlDbType.Int)
        G5V_4.Direction = Data.ParameterDirection.Input
        Dim G5T_5 As SqlParameter = cmdTest.Parameters.Add("@G5T_5", Data.SqlDbType.Int)
        G5T_5.Direction = Data.ParameterDirection.Input
        Dim G5V_6 As SqlParameter = cmdTest.Parameters.Add("@G5V_6", Data.SqlDbType.Int)
        G5V_6.Direction = Data.ParameterDirection.Input
        Dim G5T_7 As SqlParameter = cmdTest.Parameters.Add("@G5T_7", Data.SqlDbType.Int)
        G5T_7.Direction = Data.ParameterDirection.Input
        Dim G5V_8 As SqlParameter = cmdTest.Parameters.Add("@G5V_8", Data.SqlDbType.Int)
        G5V_8.Direction = Data.ParameterDirection.Input
        Dim G5T_9 As SqlParameter = cmdTest.Parameters.Add("@G5T_9", Data.SqlDbType.Int)
        G5T_9.Direction = Data.ParameterDirection.Input
        Dim G5V_10 As SqlParameter = cmdTest.Parameters.Add("@G5V_10", Data.SqlDbType.Int)
        G5V_10.Direction = Data.ParameterDirection.Input
        Dim G5T_11 As SqlParameter = cmdTest.Parameters.Add("@G5T_11", Data.SqlDbType.Int)
        G5T_11.Direction = Data.ParameterDirection.Input
        Dim G5V_12 As SqlParameter = cmdTest.Parameters.Add("@G5V_12", Data.SqlDbType.Int)
        G5V_12.Direction = Data.ParameterDirection.Input
        Dim G5T_13 As SqlParameter = cmdTest.Parameters.Add("@G5T_13", Data.SqlDbType.Int)
        G5T_13.Direction = Data.ParameterDirection.Input
        Dim G5V_14 As SqlParameter = cmdTest.Parameters.Add("@G5V_14", Data.SqlDbType.Int)
        G5V_14.Direction = Data.ParameterDirection.Input
        Dim G5T_15 As SqlParameter = cmdTest.Parameters.Add("@G5T_15", Data.SqlDbType.Int)
        G5T_15.Direction = Data.ParameterDirection.Input
        Dim G5V_16 As SqlParameter = cmdTest.Parameters.Add("@G5V_16", Data.SqlDbType.Int)
        G5V_16.Direction = Data.ParameterDirection.Input

        G1.Value = mG1
        G2T_1.Value = mG2T_1
        G2V_2.Value = mG2V_2

        G3T_1.Value = mG3T_1
        G3V_2.Value = mG3V_2
        G3T_3.Value = mG3T_3
        G3V_4.Value = mG3V_4

        G4T_1.Value = mG4T_1
        G4V_2.Value = mG4V_2
        G4T_3.Value = mG4T_3
        G4V_4.Value = mG4V_4
        G4T_5.Value = mG4T_5
        G4V_6.Value = mG4V_6
        G4T_7.Value = mG4T_7
        G4V_8.Value = mG4V_8

        G5T_1.Value = mG5T_1
        G5V_2.Value = mG5V_2
        G5T_3.Value = mG5T_3
        G5V_4.Value = mG5V_4
        G5T_5.Value = mG5T_5
        G5V_6.Value = mG5V_6
        G5T_7.Value = mG5T_7
        G5V_8.Value = mG5V_8
        G5T_9.Value = mG5T_9
        G5V_10.Value = mG5V_10
        G5T_11.Value = mG5T_11
        G5V_12.Value = mG5V_12
        G5T_13.Value = mG5T_13
        G5V_14.Value = mG5V_14
        G5T_15.Value = mG5T_15
        G5V_16.Value = mG5V_16

        Try


            cnBKTest.Open()
            Dim myReader As SqlDataReader = cmdTest.ExecuteReader()
            cnBKTest.Close()

        Catch ex As Exception
            cnBKTest.Close()
            salto = 1
        End Try
        If salto = 1 Then


            'Dim SConnStr As String = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            salto = 0
            Dim cnBKTest1 As New SqlConnection(SConnStr)
            Dim cmdTest1 As New SqlCommand("SP_GenealogiaUpdate", cnBKTest1)
            cmdTest1.CommandType = Data.CommandType.StoredProcedure

            Dim nG1 As SqlParameter = cmdTest1.Parameters.Add("@mG1", Data.SqlDbType.Int)
            nG1.Direction = Data.ParameterDirection.Input
            Dim nG2T_1 As SqlParameter = cmdTest1.Parameters.Add("@mG2T_1", Data.SqlDbType.Int)
            nG2T_1.Direction = Data.ParameterDirection.Input
            Dim nG2V_2 As SqlParameter = cmdTest1.Parameters.Add("@mG2V_2", Data.SqlDbType.Int)
            nG2V_2.Direction = Data.ParameterDirection.Input

            Dim nG3T_1 As SqlParameter = cmdTest1.Parameters.Add("@mG3T_1", Data.SqlDbType.Int)
            nG3T_1.Direction = Data.ParameterDirection.Input
            Dim nG3V_2 As SqlParameter = cmdTest1.Parameters.Add("@mG3V_2", Data.SqlDbType.Int)
            nG3V_2.Direction = Data.ParameterDirection.Input
            Dim nG3T_3 As SqlParameter = cmdTest1.Parameters.Add("@mG3T_3", Data.SqlDbType.Int)
            nG3T_3.Direction = Data.ParameterDirection.Input
            Dim nG3V_4 As SqlParameter = cmdTest1.Parameters.Add("@mG3V_4", Data.SqlDbType.Int)
            nG3V_4.Direction = Data.ParameterDirection.Input


            Dim nG4T_1 As SqlParameter = cmdTest1.Parameters.Add("@mG4T_1", Data.SqlDbType.Int)
            nG4T_1.Direction = Data.ParameterDirection.Input
            Dim nG4V_2 As SqlParameter = cmdTest1.Parameters.Add("@mG4V_2", Data.SqlDbType.Int)
            nG4V_2.Direction = Data.ParameterDirection.Input
            Dim nG4T_3 As SqlParameter = cmdTest1.Parameters.Add("@mG4T_3", Data.SqlDbType.Int)
            nG4T_3.Direction = Data.ParameterDirection.Input
            Dim nG4V_4 As SqlParameter = cmdTest1.Parameters.Add("@mG4V_4", Data.SqlDbType.Int)
            nG4V_4.Direction = Data.ParameterDirection.Input
            Dim nG4T_5 As SqlParameter = cmdTest1.Parameters.Add("@mG4T_5", Data.SqlDbType.Int)
            nG4T_5.Direction = Data.ParameterDirection.Input
            Dim nG4V_6 As SqlParameter = cmdTest1.Parameters.Add("@mG4V_6", Data.SqlDbType.Int)
            nG4V_6.Direction = Data.ParameterDirection.Input
            Dim nG4T_7 As SqlParameter = cmdTest1.Parameters.Add("@mG4T_7", Data.SqlDbType.Int)
            nG4T_7.Direction = Data.ParameterDirection.Input
            Dim nG4V_8 As SqlParameter = cmdTest1.Parameters.Add("@mG4V_8", Data.SqlDbType.Int)
            nG4V_8.Direction = Data.ParameterDirection.Input

            Dim nG5T_1 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_1", Data.SqlDbType.Int)
            nG5T_1.Direction = Data.ParameterDirection.Input
            Dim nG5V_2 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_2", Data.SqlDbType.Int)
            nG5V_2.Direction = Data.ParameterDirection.Input
            Dim nG5T_3 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_3", Data.SqlDbType.Int)
            nG5T_3.Direction = Data.ParameterDirection.Input
            Dim nG5V_4 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_4", Data.SqlDbType.Int)
            nG5V_4.Direction = Data.ParameterDirection.Input
            Dim nG5T_5 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_5", Data.SqlDbType.Int)
            nG5T_5.Direction = Data.ParameterDirection.Input
            Dim nG5V_6 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_6", Data.SqlDbType.Int)
            nG5V_6.Direction = Data.ParameterDirection.Input
            Dim nG5T_7 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_7", Data.SqlDbType.Int)
            nG5T_7.Direction = Data.ParameterDirection.Input
            Dim nG5V_8 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_8", Data.SqlDbType.Int)
            nG5V_8.Direction = Data.ParameterDirection.Input
            Dim nG5T_9 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_9", Data.SqlDbType.Int)
            nG5T_9.Direction = Data.ParameterDirection.Input
            Dim nG5V_10 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_10", Data.SqlDbType.Int)
            nG5V_10.Direction = Data.ParameterDirection.Input
            Dim nG5T_11 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_11", Data.SqlDbType.Int)
            nG5T_11.Direction = Data.ParameterDirection.Input
            Dim nG5V_12 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_12", Data.SqlDbType.Int)
            nG5V_12.Direction = Data.ParameterDirection.Input
            Dim nG5T_13 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_13", Data.SqlDbType.Int)
            nG5T_13.Direction = Data.ParameterDirection.Input
            Dim nG5V_14 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_14", Data.SqlDbType.Int)
            nG5V_14.Direction = Data.ParameterDirection.Input
            Dim nG5T_15 As SqlParameter = cmdTest1.Parameters.Add("@mG5T_15", Data.SqlDbType.Int)
            nG5T_15.Direction = Data.ParameterDirection.Input
            Dim nG5V_16 As SqlParameter = cmdTest1.Parameters.Add("@mG5V_16", Data.SqlDbType.Int)
            nG5V_16.Direction = Data.ParameterDirection.Input

            nG1.Value = mG1
            nG2T_1.Value = mG2T_1
            nG2V_2.Value = mG2V_2

            nG3T_1.Value = mG3T_1
            nG3V_2.Value = mG3V_2
            nG3T_3.Value = mG3T_3
            nG3V_4.Value = mG3V_4

            nG4T_1.Value = mG4T_1
            nG4V_2.Value = mG4V_2
            nG4T_3.Value = mG4T_3
            nG4V_4.Value = mG4V_4
            nG4T_5.Value = mG4T_5
            nG4V_6.Value = mG4V_6
            nG4T_7.Value = mG4T_7
            nG4V_8.Value = mG4V_8

            nG5T_1.Value = mG5T_1
            nG5V_2.Value = mG5V_2
            nG5T_3.Value = mG5T_3
            nG5V_4.Value = mG5V_4
            nG5T_5.Value = mG5T_5
            nG5V_6.Value = mG5V_6
            nG5T_7.Value = mG5T_7
            nG5V_8.Value = mG5V_8
            nG5T_9.Value = mG5T_9
            nG5V_10.Value = mG5V_10
            nG5T_11.Value = mG5T_11
            nG5V_12.Value = mG5V_12
            nG5T_13.Value = mG5T_13
            nG5V_14.Value = mG5V_14
            nG5T_15.Value = mG5T_15
            nG5V_16.Value = mG5V_16









            cnBKTest1.Open()
            Dim myReader1 As SqlDataReader = cmdTest1.ExecuteReader()
            cnBKTest1.Close()


        End If









    End Sub
    Public Sub ClavesDePadres(ByVal idAnimal As Integer)
        If idAnimal <> 0 Then
            Dim SConnStr As String = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            Dim cnBKTest As New SqlConnection(SConnStr)
            Dim cmdTest As New SqlCommand("SP_GenealogiaSelect", cnBKTest)
            cmdTest.CommandType = Data.CommandType.StoredProcedure

            Dim AnimalId As SqlParameter = cmdTest.Parameters.Add("@AnimalId", Data.SqlDbType.Int)
            AnimalId.Direction = Data.ParameterDirection.Input
            Dim paternoId As SqlParameter = cmdTest.Parameters.Add("@paternoId", Data.SqlDbType.Int)
            paternoId.Direction = Data.ParameterDirection.Output
            Dim maternoID As SqlParameter = cmdTest.Parameters.Add("@maternoId", Data.SqlDbType.Int)
            maternoID.Direction = Data.ParameterDirection.Output

            AnimalId.Value = idAnimal
            cnBKTest.Open()
            Dim myReader As SqlDataReader = cmdTest.ExecuteReader()
            padre = paternoId.Value
            madre = maternoID.Value
            cnBKTest.Close()
        Else
            padre = 0
            madre = 0
        End If
    End Sub
End Class
